package com.yu.footballpromotionliveadmin.service.impl;

import com.yu.footballpromotionliveadmin.dao.CompetitionRecordDao;
import com.yu.footballpromotionliveadmin.dto.DTO;
import com.yu.footballpromotionliveadmin.dto.MessageEnum;
import com.yu.footballpromotionliveadmin.page.PageInfo;
import com.yu.footballpromotionliveadmin.pojo.CompetitionRecord;
import com.yu.footballpromotionliveadmin.service.CompetitionRecordService;
import com.yu.footballpromotionliveadmin.vo.CompetitionRecordView;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Optional;

@Service
public class CompetitionRecordServiceImpl implements CompetitionRecordService {

    @Autowired
    CompetitionRecordDao competitionRecordDao;


    @Override
    public DTO<CompetitionRecord> getAllCompetitionRecords() throws Exception {
        List<CompetitionRecord> activityTypes = competitionRecordDao.getAllCompetitionRecors();
        //isPresent表示判断不为空成立则dto为？与：之间的对象
        MessageEnum messageEnum = Optional.ofNullable(activityTypes).isPresent() ? MessageEnum.GETADMINSUCCESS : MessageEnum.GETADMINFAIL;

        return new DTO<CompetitionRecord>(messageEnum.getCode(),messageEnum.getMessage(),activityTypes);
    }

    @Override
    public DTO<CompetitionRecord> getCompetitionRecordById(String actno) throws Exception {

        CompetitionRecord activityType = competitionRecordDao.getCompetitionRecorByUno(actno);

        MessageEnum messageEnum = Optional.ofNullable(activityType).isPresent() ? MessageEnum.GETADMINSUCCESS : MessageEnum.GETADMINFAIL;
        return new DTO<CompetitionRecord>(messageEnum.getCode(),messageEnum.getMessage(),activityType);
    }

    @Override
    public DTO<CompetitionRecord> deleteCompetitionRecord(String actno) throws Exception {
        Integer integer = competitionRecordDao.deleteCompetitionRecor(actno);

        MessageEnum messageEnum = integer > 0 ? MessageEnum.DELETEADMINSUCCESS : MessageEnum.DELETEADMINFAIL;
        return new DTO<CompetitionRecord>(messageEnum.getCode(),messageEnum.getMessage());
    }

    @Override
    public DTO<CompetitionRecord> updateCompetitionRecord(CompetitionRecordView activityType) throws Exception {
        CompetitionRecord competitionRecord = dataFormat(activityType);

        Integer integer = competitionRecordDao.updateCompetitionRecor(competitionRecord);

        MessageEnum messageEnum = integer > 0 ? MessageEnum.UPDATEADMINSUCCESS : MessageEnum.UPDATEADMINFAIL;
        return new DTO<CompetitionRecord>(messageEnum.getCode(),messageEnum.getMessage());
    }

    @Override
    public DTO<CompetitionRecord> addCompetitionRecord(CompetitionRecordView activityTypeview) throws Exception {
        CompetitionRecord competitionRecord = dataFormat(activityTypeview);

        Integer integer = competitionRecordDao.addCompetitionRecorByUno(competitionRecord);

        MessageEnum messageEnum = integer > 0 ? MessageEnum.ADDADMINSUCCESS : MessageEnum.ADDADMINFAIL;
        return new DTO<CompetitionRecord>(messageEnum.getCode(),messageEnum.getMessage(),competitionRecord);
    }

    @Override
    public DTO<CompetitionRecord> getCompetitionRecordsByPage(PageInfo pageInfo) throws Exception {
        List<CompetitionRecord> activityTypesByPage = competitionRecordDao.getCompetitionRecorsByPage(pageInfo);
        MessageEnum messageEnum = Optional.ofNullable(activityTypesByPage).isPresent() ? MessageEnum.GETADMINSUCCESS : MessageEnum.GETADMINFAIL;

        return new DTO<CompetitionRecord>(messageEnum.getCode(),messageEnum.getMessage(),activityTypesByPage);
    }

    @Override
    public DTO<CompetitionRecord> getCompetitionRecordsWithOrders(String actno) throws Exception {
        return null;
    }

    @Override
    public DTO<CompetitionRecord> activate(String actno) throws Exception {
        return null;
    }

    @Override
    public DTO<CompetitionRecord> lettory(String actno, String giftName) throws Exception {
        return null;
    }

    public CompetitionRecord dataFormat(CompetitionRecordView competitionRecordView) throws ParseException {
        CompetitionRecord competitionRecord = new CompetitionRecord();
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        competitionRecord.setCrtime(format.parse(competitionRecordView.getCrtime()));
        competitionRecord.setHtn(competitionRecordView.getHtn());
        competitionRecord.setLbano(competitionRecordView.getLbano());
        competitionRecord.setRcw(competitionRecordView.getRcw());
        competitionRecord.setRcno(competitionRecordView.getRcno());
        competitionRecord.setTno(competitionRecordView.getTno());
        competitionRecord.setRcplace(competitionRecordView.getRcplace());
        competitionRecord.setRcetime(format.parse(competitionRecordView.getRcetime()));
        competitionRecord.setCrno(competitionRecordView.getCrno());
        return competitionRecord;
    }
}
